package com.atguigu.crowd.handler;

import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.po.MemberPO;
import com.atguigu.crowd.entity.vo.MemberAuthVO;
import com.atguigu.crowd.entity.vo.PortalTypeVO;
import com.atguigu.crowd.entity.vo.ProjectVO;
import com.atguigu.crowd.service.api.MemberService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MemberProviderHandler {

    @Autowired
    private MemberService memberService;


    //根据memberId获取此用户是否已经有发起人信息
    @RequestMapping("/get/member/launch")
    public ResultEntity<Boolean>getInfoByMemberIdFromLaunch(@RequestParam Integer memberId){
        try {
            //成功 没有相同的账号
            boolean memberLaunch = memberService.getMemberLaunch(memberId);
            return ResultEntity.successWithData(memberLaunch);
        }catch (Exception e){
            //失败 有相同的账号
            if(e instanceof DuplicateKeyException){
                return ResultEntity.failed(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }
            return ResultEntity.failed(e.getMessage());
        }
    }

    //注册保存  在此之前将VO转成PO
    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMember(
            @RequestBody MemberPO memberPO
    ){
        try {
            //成功 没有相同的账号
            memberService.saveMember(memberPO);
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            //失败 有相同的账号
            if(e instanceof DuplicateKeyException){
                return ResultEntity.failed(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }
            return ResultEntity.failed(e.getMessage());
        }
    }




    /**
     * 根据账号查询
     *
     * @param loginacct
     * @return
     */
    @RequestMapping("/get/memberpo/by/login/acct/remote")
    public ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(@RequestParam("loginacct") String loginacct) {


        try {
            // 1.调用本地Service完成查询
            MemberPO memberPO = memberService.getMemberPOByLoginAcct(loginacct);

            // 2.如果没有抛异常，那么就返回成功的结果
            return ResultEntity.successWithData(memberPO);
        } catch (Exception e) {
            e.printStackTrace();

            // 3.如果捕获到异常则返回失败的结果
            if(e.getMessage().equals("Index: 0, Size: 0")){
                return ResultEntity.failed("查无此账号");
            }
            return ResultEntity.failed(e.getMessage());

        }

    }


    //保存用户认证信息
    @RequestMapping("/save/member/auth")
    ResultEntity<String> saveMemberAuthVORemote(@RequestBody MemberAuthVO memberAuthVO){
        try {
            // 调用“本地”Service执行保存
            memberService.saveMemberAuth(memberAuthVO);
            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }
    }
}